

const {GeneratedType, Registry} = require("@cosmjs/proto-signing");
const { DirectSecp256k1HdWallet, DirectSecp256k1Wallet } = require("@cosmjs/proto-signing")
const { SigningStargateClient, StargateClient } = require("@cosmjs/stargate")
const { stringToPath } = require("@cosmjs/crypto")
const config = require('./config')

let rpc = config.noble.url;

rpc = "https://noble-testnet-rpc.polkachu.com"
console.log('rpc: ', rpc);

const wallet_caller = async ()=>{
    const mnemonic = config.words;
    const wallet1 = await DirectSecp256k1HdWallet.fromMnemonic(
        mnemonic,
        {
            hdPaths: [stringToPath(config.noble.accounts.caller.hdPaths)],
            prefix: "noble"
        }
    );

    const [account] = await wallet1.getAccounts();

    console.log(account.address)

    const client = await StargateClient.connect(rpc)
    console.log("With client, chain id:", await client.getChainId(), ", height:", await client.getHeight())
    console.log(
        account.address, "- balances:",
        await client.getAllBalances(account.address));




}
const wallet_receipter = async ()=>{
    const mnemonic = config.words;
    const wallet1 = await DirectSecp256k1HdWallet.fromMnemonic(
        mnemonic,
        {
            hdPaths: [stringToPath(config.noble.accounts.receipter.hdPaths)],
            prefix: "noble"
        }
    );
    const [account] = await wallet1.getAccounts();

    console.log(account.address)

    const client = await StargateClient.connect(rpc)
    console.log("With client, chain id:", await client.getChainId(), ", height:", await client.getHeight())
    console.log(
        account.address, "- balances:",
        await client.getAllBalances(account.address));
}

const wallet_prikey = async ()=> {
    try{
        let PRIVATE_KEY="724e6d2de114fbc312e6c84afa215f3ccc3dedaa870f1dea0d1aeb69131a6f1c";
        let privKey = Buffer.from(PRIVATE_KEY,'hex');
        const wallet = await DirectSecp256k1Wallet.fromKey(privKey,"noble");
        const [account] = await wallet.getAccounts();

        console.log(account.address)
        const client = await StargateClient.connect(rpc)
        console.log("With client, chain id:", await client.getChainId(), ", height:", await client.getHeight())
        console.log(
        account.address, "- balances:",
        await client.getAllBalances(account.address));



        let balances = await client.getAllBalances("noble1cnfqqhjgu850cqcjxypwvj0u5ty9adlj9n38jy");
        console.log('balance: ', balances);

        let ret = await client.getHeight();
        console.log("ret: ", ret);


    }catch(e){
        console.log("e: ",e);
    }

}
const sendAmount = async ()=> {
    const mnemonic = config.words;
    const wallet1 = await DirectSecp256k1HdWallet.fromMnemonic(
        mnemonic,
        {
            hdPaths: [stringToPath(config.noble.accounts.caller.hdPaths)],
            prefix: "noble"
        }
    );

    const [account1] = await wallet1.getAccounts();

    console.log(account1.address)

    let PRIVATE_KEY="724e6d2de114fbc312e6c84afa215f3ccc3dedaa870f1dea0d1aeb69131a6f1c";
        let privKey = Buffer.from(PRIVATE_KEY,'hex');
        const wallet = await DirectSecp256k1Wallet.fromKey(privKey,"noble");
        const [account2] = await wallet.getAccounts();

        const client_caller = await SigningStargateClient.connectWithSigner(
            config.noble.url,
            wallet1,
            {gasPrice:"1uusdc"}

        );

        console.log(
        account1.address, "- old balances:",
        await client_caller.getAllBalances(account1.address));

        const sendMsg = {
            typeUrl: "/cosmos.bank.v1beta1.MsgSend",
            value:{
                fromAddress: account1.address,
                toAddress:account2.address,
                amount: [
                    {
                        denom: "uusdc",
                        amount: "1",
                    },
                ],
    
            }
        }
        const fee = {
            amount: [

            ],
            gas: "200000",
        };
        const memo = "haolf";
    
    
    
        const result = await client_caller.signAndBroadcast(
            account1.address,
            [sendMsg],
            "auto",
            memo
        );
        console.log("Burned on Noble to to https://testnet.mintscan.io/noble-testnet/tx/" + result.transactionHash)


        console.log(
        account1.address, "- new balances:",
        await client_caller.getAllBalances(account1.address));

}

const main = async() => {
    await wallet_caller();
    //await wallet_receipter();
    //await wallet_prikey();
    await sendAmount();
}
main();